package com.csust.automaticteach.app.tools;

/**
 * @ClassNameExecuteJavaCodeTool
 * @Description TODO
 * @Author钰玟
 * @Date2025/7/30 上午10:38
 * @Version 1.0
 **/

import com.csust.automaticteach.judge.codesandbox.CodeSandboxFactory;
import com.csust.automaticteach.judge.codesandbox.model.ExecuteCodeRequest;
import com.csust.automaticteach.judge.codesandbox.model.ExecuteCodeResponse;
import com.csust.automaticteach.judge.codesandbox.model.JudgeInfo;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

/**
 * 执行java代码的工具
 */
public class ExecuteJavaCodeTool {


    private final String type;//代码沙箱种类的指定

    public ExecuteJavaCodeTool(String type) {
        this.type = type;
    }

    @Tool(description = "Execute Java code: Compile source files to .class via , run with (args passed to main method's )")
    public String executeJavaCode(@ToolParam(description = "the java code to execute") String javaCode,
                                  @ToolParam(description = "the input of example,This is an inputList format:[1 2,3 2]") List<String> inputList) {
        CodeSandboxFactory.newInstance(type);
        ExecuteCodeResponse executeCodeResponse = CodeSandboxFactory.newInstance(type).executeCode(
                ExecuteCodeRequest.builder()
                        .code(javaCode)
                        .language("java")
                        .inputList(inputList)
                        .build()
        );
        List<String> outputList = executeCodeResponse.getOutputList();
        String message = executeCodeResponse.getMessage();
        Integer status = executeCodeResponse.getStatus();
        JudgeInfo judgeInfo = executeCodeResponse.getJudgeInfo();
        return "Output: " + outputList + "\n" + "Message: " + message + "\n" + "Status: " + status + "\n" + "JudgeInfo: " + judgeInfo;
    }
}
